﻿@page "/bbs/postform"
@inherits BaseComponent

<div class="container cl-container cl-bbs">
    <UCategory Service="Service" />
    <CmsCard Class="cl-bbs-form" Icon="far fa-question-circle" Title="提问题">
        @if (!string.IsNullOrWhiteSpace(errorMessage))
        {
            <AntAlert Type="error">@errorMessage</AntAlert>
        }
        <AntForm Id="@formId">
            <div class="label"><span style="color:#f00;">*</span>标题</div>
            <AntInput Id="@nameof(PostFormInfo.Title)" Value="@model.Title" Placeholder="请输入标题" Required />
            <div class="label"><span style="color:#f00;">*</span>请详细描述你的问题</div>
            <CmsMarkdown Id="@nameof(PostFormInfo.Content)" Value="@model.Content" Placeholder="在这里输入问题内容" />
            <div class="button">
                <AntButton Name="发布" OnClick="@($"return AntBlazor.validate('{formId}');")" />
                <AntButton Name="取消" Type="default" OnClick="location='./bbs';return false;" />
            </div>
        </AntForm>
    </CmsCard>
    <div>
        <SigninBox />
        <div style="height:20px;"></div>
        <CmsCard Title="如何排版">

        </CmsCard>
    </div>
</div>

@code {
    private ISiteService Service;
    private PostFormInfo model = new();
    private string formId = "post-form";
    private string errorMessage = "";

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        Service = await CreateServiceAsync<ISiteService>();
        if (Context.Request.IsHandler(formId))
        {
            model = Context.Request.GetModel<PostFormInfo>();
            var result = await Service.SavePostAsync(model);
            if (!result.IsValid)
                errorMessage = result.Message;
            else
                Context.Response.Redirect(Url.UserPostsUrl.Trim('.'));
        }
    }
}